package com.kaili.basic.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.PickManagedto;
import com.kaili.basic.pojo.dto.WellenAndPickManagedto;
import com.kaili.basic.pojo.dto.Wellendto;
import com.kaili.basic.pojo.entity.PickManage;
import com.kaili.basic.pojo.vo.OutApplicationProductVO;
import com.kaili.basic.pojo.vo.WellenVo;
import com.kaili.basic.service.PickManageService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Api(tags = "拣货管理", description = "拣货管理相关API")
public class PickManageController {

    Logger log = LoggerFactory.getLogger(PickManageController.class);

    @Autowired
    private PickManageService pickManageService;

    @GetMapping("/pickManage")
    @ApiOperation(value = "查询出库申请单", notes = "查询出库申请单")
    public Page<PickManage> queryOutApplicationForm(@ModelAttribute PickManagedto pickManagedto){
        PickManage pickManage = new PickManage();
        pickManage.setShipper(pickManagedto.getShipper());
        pickManage.setShipperName(pickManagedto.getShipperName());
        pickManage.setRequestNo(pickManagedto.getRequestNo());
        pickManage.setPage(pickManagedto.getPage());
        pickManage.setPageSize(pickManagedto.getPageSize());
        return pickManageService.queryOutApplicationByPage(new Page<PickManage>(pickManage.getPage(),pickManage.getPageSize()),pickManage);
    }

    @GetMapping("/pickManageProduct/{applyId}")
    @ApiOperation(value = "根据出库申请单查询产品列表", notes = "查询出库申请单")
    public List<OutApplicationProductVO> queryOutApplicationProduct(@PathVariable Integer applyId){
        return pickManageService.queryOutApplicationProduct(applyId);
    }

    @PostMapping("/wellen")
    @ApiOperation(value = "生成波次单",notes = "生成波次单")
    public CommonStatus createWellen(@RequestBody List<WellenAndPickManagedto> wellenAndPickManagedtos){
        if (wellenAndPickManagedtos != null && !wellenAndPickManagedtos.isEmpty() ){
            if (wellenAndPickManagedtos.size() < 2){
                return CommonStatus.WELLEN_NUMBER_LOSE;
            }
            for (WellenAndPickManagedto wellenAndPickManagedto:wellenAndPickManagedtos
                 ) {
                //根据入库申请ID查询波次单号是否存在,存在不添加
                Integer wellenNo = pickManageService.getWellenNoByApplyId(wellenAndPickManagedto.getApplyId());
                if (wellenNo != null){
                    return CommonStatus.WELLEN_ADD_REPEAT;
                }
                if (wellenAndPickManagedto.getShipper() != wellenAndPickManagedtos.get(0).getShipper()){
                    return CommonStatus.WELLEN_ADD_MUST_EQUALLY;
                }
            }
            //生成波次单
            pickManageService.createWellen(wellenAndPickManagedtos);
            return CommonStatus.WELLEN_ADD_SUCCESS;
        }else{
            return CommonStatus.WELLEN_ADD_FAILE;
        }
    }

    @GetMapping("/wellen")
    @ApiOperation(value = "查询波次单",notes = "查询波次单")
    public Page<WellenVo> queryWellenByPages(@ModelAttribute Wellendto wellendto){
        WellenVo wellenVo = new WellenVo();
        wellenVo.setWellenNo(wellendto.getWellenNo());
        wellenVo.setWellenStatus(wellendto.getWellenStatus());
        wellenVo.setPickStartTime(wellendto.getStartTime());
        wellenVo.setPickEndTime(wellendto.getEndTime());
        wellenVo.setPage(wellendto.getPage());
        wellenVo.setPageSize(wellendto.getPageSize());

        return pickManageService.queryWellenByPages(new Page<WellenVo>(wellenVo.getPage(),wellenVo.getPageSize()),wellenVo);
    }

    @DeleteMapping("/wellen/{wellenId}")
    @ApiOperation(value = "删除波次单",notes = "删除波次单")
    public CommonStatus deleteWellen(@PathVariable Long wellenId){
        //逻辑删除
        try{
            pickManageService.deleteWellenByWellenId(wellenId);
            return CommonStatus.DELETE_OK;
        }catch (Exception e){
            e.printStackTrace();
            return CommonStatus.DELETE_FAIL;
        }

    }


    @PostMapping("/wellen")
    @ApiOperation(value = "修改波次单",notes = "修改波次单")
    public CommonStatus updateWellen(@ModelAttribute Wellendto wellendto){

        pickManageService.updateWellen(wellendto);

        return CommonStatus.UPDATE_OK;
    }


    @GetMapping("/wellenExport")
    @ApiOperation(value = "波次单导出",notes = "波次单导出")
    public void queryWellenByExport(@ModelAttribute Wellendto wellendto, HttpServletResponse response){
        WellenVo wellenVo = new WellenVo();
        wellenVo.setWellenNo(wellendto.getWellenNo());
        wellenVo.setWellenStatus(wellendto.getWellenStatus());
        wellenVo.setPickStartTime(wellendto.getStartTime());
        wellenVo.setPickEndTime(wellendto.getEndTime());
        wellenVo.setPage(wellendto.getPage());
        wellenVo.setPageSize(wellendto.getPageSize());
        try{
            ExcelUtil.exportExcel(pickManageService.queryWellenByExport(wellenVo),"波次单管理","波次单管理",WellenVo.class,"波次单管理"+UUIDUtil.getUUId()+".xls",response);
        }catch (RestException e){
            log.error("波次单导出异常"+e.getMessage());
        }
    }


}
